﻿using Gargoyle.Roton;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Lyon
{
    internal static class Common
    {
        internal static Int16 EngineID;
        internal static string ErrorMessage;
        internal static string ErrorStackTrace;
        internal static string GameFile;
        internal static bool WideMode;

        /// <summary>
        /// Indicates a filename to load by default.
        /// </summary>
        public static string WorldFilename { get; set; }

        internal static void DisplayError(object sender)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("Roton has crashed:");
            builder.AppendLine(ErrorMessage);
            builder.AppendLine();
            builder.Append("Would you like a stack trace copied to your clipboard?");
            if(MessageBox.Show(builder.ToString(), "Crash", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Clipboard.SetText(ErrorStackTrace);
            }
        }

        private static Int16 GetWorldID(byte[] bytes)
        {
            using(MemoryStream mem = new MemoryStream(bytes))
            {
                BinaryReader reader = new BinaryReader(mem);
                return reader.ReadInt16();
            }
        }

        internal static Cache LoadWorld()
        {
            string FileName = "";
            Cache Result;
            if(WorldFilename != null && File.Exists(WorldFilename))
                FileName = WorldFilename;
            else
            {
                using(OpenFileDialog OFD = new OpenFileDialog())
                {
                    OFD.AddExtension = true;
                    OFD.AutoUpgradeEnabled = true;
                    OFD.CheckFileExists = true;
                    OFD.CheckPathExists = true;
                    OFD.Filter = "World Files (*.zzt, *.szt)|*.zzt;*.szt;*.ZZT;*.SZT";
                    OFD.FilterIndex = 1;
                    OFD.Multiselect = false;
                    OFD.ShowHelp = false;
                    OFD.ShowReadOnly = false;
                    OFD.SupportMultiDottedExtensions = true;
                    OFD.Title = "Open World";
                    OFD.ValidateNames = true;

                    if(OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        FileName = OFD.FileName;
                }
            }

            if(FileName != "")
            {
                byte[] FileData = File.ReadAllBytes(FileName);
                EngineID = GetWorldID(FileData);
                WideMode = (EngineID == -2);
                Result = new Cache(Path.GetDirectoryName(FileName));
                Result.SaveFile(Path.GetFileName(FileName), FileData, false);
                GameFile = Path.GetFileNameWithoutExtension(FileName);
                return Result;
            }

            return null;
        }

        internal static string ShowSaveScreenshotDialog()
        {
            using(SaveFileDialog SFD = new SaveFileDialog())
            {
                SFD.AddExtension = true;
                SFD.AutoUpgradeEnabled = true;
                SFD.Filter = "PNG Screenshots (*.png)|*.png;*.PNG";
                SFD.FilterIndex = 1;
                SFD.OverwritePrompt = true;
                SFD.Title = "Save Screenshot";
                if(SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    return SFD.FileName;
                else
                    return null;
            }
        }
    }
}
